package com.hyw.supply.service.impl;

import com.hyw.supply.model.entity.SupplyOrder;
import com.hyw.supply.mapper.SupplyOrderMapper;
import com.hyw.supply.service.SupplyOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 * 供应商订单信息 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2024-04-18
 */
@Service
public class SupplyOrderServiceImpl extends ServiceImpl<SupplyOrderMapper, SupplyOrder> implements SupplyOrderService {


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(Collection<SupplyOrder> entityList, int batchSize) {
        try {
            int size = entityList.size();
            int idxLimit = Math.min(batchSize, size);
            int i = 1;
            //保存单批提交的数据集合
            List<SupplyOrder> oneBatchList = new ArrayList<>();
            for (Iterator<SupplyOrder> it = entityList.iterator(); it.hasNext(); ++i) {
                SupplyOrder element = it.next();
                oneBatchList.add(element);
                if (i == idxLimit) {
                    baseMapper.insertBatchSomeColumn(oneBatchList);
                    //每次提交后需要清空集合数据
                    oneBatchList.clear();
                    idxLimit = Math.min(idxLimit + batchSize, size);
                }
            }
        } catch (Exception e) {
            log.error("saveBatch fail", e);
            return false;
        }
        return true;
    }
}

